#include <iostream>
#include <config/config_client/XConfigClient.h>
#include <xplatform/XMsg.h>
#include <xplatform/XTools.h>
#include <xplatform/protobuf/xmsg_com.pb.h>
#include <thread>
#include <register/register_client/xregister_client.h>
#include "XDirConfig.pb.h"
#define REG XRegisterClient::Get()
#define CONFIG XConfigClient::Get()
using namespace std;
using namespace xmsg;
int main(int argc, char** argv){
    int client_port = 4000;
    // 设置注册中心的IP地址和端口
    REG->setServerIP("127.0.0.1");
    REG->setServerPort(client_port);
    REG->RegisterServer("test_config",client_port,nullptr);
    this_thread::sleep_for(1s);
    auto configs = REG->GetService(CONFIG_NAME,1);
    std::cout<<configs.DebugString()<<endl;
    if(configs.services_size()<=0){
        return -1;
    }
    auto conf = configs.services(0);
    if(conf.ip().empty()||conf.port()<=0){
        return -1;
    }
    XDirConfig tmp_conf;
    CONFIG->StartGetConfig(conf.ip().c_str(),conf.port(),nullptr,client_port,&tmp_conf);
    this_thread::sleep_for(200ms);
    
    string proto;
    string s;
    auto message = CONFIG->LoadPoto("XDirConfig.proto","XDirConfig",s);
    auto ref =message->GetReflection();
    auto descriptor = message->GetDescriptor();
    auto FindField=descriptor->FindFieldByName("root");
    ref->SetString(message,FindField,"\\test_new_root\\");
    cout<<message->DebugString()<<endl;
    //     XCOM_LOG_DEBUG("hello world");
//     XConfigClient::Get()->setServerIP("127.0.0.1");
//     XConfigClient::Get()->setServerPort(CONFIG_PORT);
//     XConfigClient::Get()->StartConnect();
//     if(!XConfigClient::Get()->waitConnected(3)){
//         XCOM_LOG_ERROR("connect config server failed");
//         return 1;
//     }
//     XCOM_LOG_ERROR("connect config server success");
//     XConfigClient::RegMsgCallback();
//     xmsg::XConfig config;
//     config.set_service_ip("127.0.0.1");
//     config.set_service_port(20018);
// #if 0
//     config.set_service_name("test");
//     config.set_private_pb(config.SerializeAsString());
//     config.set_proto("test");
//     XConfigClient::Get()->SendConfig(&config);
// #else
//     XConfigClient::Get()->LoadConfig("127.0.0.1",20018);
//     std::this_thread::sleep_for(std::chrono::seconds(1));
//     config = xmsg::XConfig();
//     XConfigClient::Get()->GetConfig("127.0.0.1",20018,&config);
//     XCOM_LOG_DEBUG(config.DebugString());
// #endif
    XConfigClient::Wait();
    return 0;
}